<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--工作区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="workspaceName"
            placeholder="请输入名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="workspaceOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-popover title="列筛选" trigger="click" style="float: right;margin-right: 10px;">
          <el-checkbox-group v-model="checkedColumns" size="mini">
            <el-checkbox style="display: block;" v-for="(item,index) in columns" :key="item.label" :label="item.label"
                         @change="chickBoxGroupChange(item)"
            >{{ item.label }}
            </el-checkbox>
          </el-checkbox-group>
          <el-button size="small" slot="reference"><i class="el-icon-arrow-down el-icon-menu"></i></el-button>
        </el-popover>

      <!--数据-->
      <common-table :list="list" @handleSelectionChange="handleSelectionChange" :options="options"
                    :columns="columns" :operates="operates">
      </common-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                  @pagination="getList"/>
    </el-col>
    </el-row>
  </div>
</template>

<script>
import CommonTable from '@/views/components/dynamicTable'
import { detailList, fileList, getDetailColumns } from '@/api/pms/workspace'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Workspace_detail",
  components: { Treeselect,CommonTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: null,
      list: [],
      // 弹出层标题
      title: "",
      // 树选项
      workspaceOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 名称
      workspaceName: undefined,
      options: {// table 的参数
        stripe: true, // 是否为斑马纹 table
        loading: false, // 是否添加表格loading加载动画
        highlightCurrentRow: true, // 是否支持当前行高亮显示
        mutiSelect: true // 是否支持列表项选中功能
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        objectId: undefined
      },
      listQuery:{
        pageNum: 1,
        pageSize: 10,
        objectId: undefined
      },
      checkedColumns: [],
      columns: [],// 需要展示的列
      // 按钮
      operates: {// 列操作按钮
        width: 150,
        list: [
          {
            id: '1',
            label: '删除',
            style: 'cursor:pointer;color: #409eff;',
            class: 'el-icon-delete',
            show: true,
            plain: false,
            method: (index, row) => {
              this.handleTo(row)
            }
          }
        ]
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    workspaceName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    const objectId = this.$route.params && this.$route.params.objectId;
    this.queryParams.objectId = objectId;
    this.getList();
    this.getColumn();
  },
  methods: {
    chickBoxGroupChange(item) {
      item.show = !item.show
    },
    /** 查询工作区 */
    getList() {
      this.loading = true;
      detailList(this.queryParams).then(response => {
          response.rows.push({id:this.queryParams.objectId,parentId:0,name:"工作区"});
          response.rows.forEach((item,index)=>{
            response.rows[index]=this.normalizer(item);
          });
          this.dataList = this.handleTree(response.rows, "id");
          this.workspaceOptions=this.dataList;
          this.loading = false;
        }
      );
    },
    //获取列
    getColumn() {
      this.loading = true
      getDetailColumns().then(response => {
        this.columns = response.data;
        this.columns.forEach((item, index) => {
          this.checkedColumns.push(item.label)
        });
        this.loading = false
      })
    },
    getFileList() {
      this.loading = true;
      fileList(this.listQuery).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 转换数据结构 */
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        parentId: node.parentId
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.listQuery.objectId = data.id;
      this.getFileList();
    },
    // 取消按钮
    // cancel() {
    //   this.open = false;
    //   this.reset();
    // },
    // // 表单重置
    // reset() {
    //   this.form = {};
    //   this.resetForm("form");
    // },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.page = 1;
    //   this.getList();
    // },
    /** 重置按钮操作 */
    // resetQuery() {
    //   this.dateRange = [];
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    }
  }
};
</script>
